package cn.hxth.dubbo.customer.controller;

import cn.hxth.api.model.OutstockRecord;
import cn.hxth.api.model.Product;
import cn.hxth.api.model.ProductBaseInfo;
import cn.hxth.api.model.ProductSpec;
import cn.hxth.api.query.OutstockRecordQueryObject;
import cn.hxth.api.query.PageResult;
import cn.hxth.api.query.QueryObject;
import cn.hxth.api.service.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pages/outstockRecord/")
public class OutstockRecordController {
    @Resource(name = "myRedisTemplate")
    private RedisTemplate<String,Object> redisTemplate ;
    @Resource
    private IConsumerService consumerService ;
    @Resource
    private IOutstockRecordService outstockRecordService ;
    @Resource
    private IProductNameService productNameService ;
    @Resource
    private IProductSpecService productSpecService ;
    @Resource
    private IProductService productService ;
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    @RequestMapping("insertOutstockRecord")
    public boolean insertOutstockRecord(OutstockRecord outstockRecord) {
        List<OutstockRecord> outstockRecords = outstockRecord.getOutstockRecords() ;
       for (OutstockRecord record:outstockRecords){

           //System.out.println(record.getConsumer().getCid());
           // 填充相关购货单位编号
           record.setConsumer(this.consumerService.getConsumer(record.getConsumer().getCid()));
           long pid = this.productService.getPidBySid(record.getProduct().getSpec().getSid());
           // 填写相关出库商品编号
           record.getProduct().setPid(pid);
           // 设置出库时间
           record.setDate(new Date());
           Product product =  record.getProduct() ;
           JSONObject jsonObject = null ;
           // 从redis中取出该出库商品聚合信息
           String jsonStr = (String) redisTemplate.opsForValue().get("dim_product_" + product.getPid());
           if(jsonStr==null){
               // 从业务层取出商品部分信息
               jsonObject = JSONObject.parseObject(this.productService.getProduct(pid)) ;
               // 取出商品对应的规格编号
               Integer sid = jsonObject.getJSONObject("spec").getInteger("sid");
               // 取得该规格相关信息
               ProductSpec spec = this.productSpecService.getProductSpec(sid) ;
              // System.out.println(JSONObject.toJSONString(spec));
               // 取得该商品规格对应的商品名称
               spec.setProductName(this.productNameService.getProductName(spec.getProductName().getNid()));
               // JSONObject specJson = JSONObject.parseObject(JSONObject.toJSONString(spec)) ;
               // 完整商品信息字符串
               jsonObject.put("spec",spec) ;
               this.redisTemplate.opsForValue().set("dim_product_"+pid,jsonObject.toJSONString());
             //  System.out.println(this.redisTemplate.opsForValue().get("dim_product_"+pid)+"******************");

           }
           else{
               // System.out.println(this.redisTemplate.opsForValue().get("dim_product_"+pid)+"*****");
               jsonObject = JSONObject.parseObject(jsonStr) ;
           }
           // 开始填入出库信息
           product.setFloor(jsonObject.getInteger("floor"));
           product.setSpec(jsonObject.getObject("spec", ProductSpec.class));
           product.setNote(jsonObject.getString("note"));
           product.setUnit(jsonObject.getString("unit"));
           product.setFin(jsonObject.getString("fin"));
           ProductBaseInfo productBaseInfo = jsonObject.getObject("product_base_info", ProductBaseInfo.class);
           record.setInventory(productBaseInfo.getInventory());
           record.setLin(productBaseInfo.getLin());
           record.setPrice(productBaseInfo.getPrice());
           record.setProductSpec(record.getProduct().getSpec().getTitle());
           record.setProductName(record.getProduct().getSpec().getProductName().getTitle());

       }
        return this.outstockRecordService.insertOutstockRecord(outstockRecords) ;
       /* for(InstockRecord record:instockRecords){
            // 为了防止读取不到对应商品读取可以读取缓存 我在控制层先把这两个值设进去
            record.getProduct().setSpec(this.productSpecService.getProductSpec(instockRecord.getProduct().getSpec().getSid(),instockRecord.getProduct().getSpec().getProductName().getNid()));
            record.getProduct().getSpec().setProductName(this.productNameService.getProductName(instockRecord.getProduct().getSpec().getProductName().getNid()));
        }*/

            //record.getProduct().getSpec().setProductName(this.productNameService.getProductName(outstockRecord.getProduct().getSpec().getProductName().getNid()));
        }
       //return this.outstockRecordService.insertOutstockRecord(outstockRecords) ;

    @RequestMapping("getOutstockRecords")
    public PageResult getOutstockRecords(OutstockRecordQueryObject outstockRecordQueryObject){
        return  this.outstockRecordService.findAll(outstockRecordQueryObject) ;
    }
    @RequestMapping("getAggrInfo")
    public  PageResult getAggrInfo(QueryObject queryObject){
        return  this.outstockRecordService.findAggrInfo(queryObject) ;
    }
}
